package xj.meta;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Annotation {

  public Annotation(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }

  public Map<String, Object> getMembers() {
    return members;
  }

  public Set<String> getMemberNames() {
    if (members != null) {
      return members.keySet();
    } else {
      return null;
    }
  }

  public boolean hasMember(String name) {
    if (name != null && members != null) {
      return members.containsKey(name);
    }
    return false;
  }

  public void setMemberValue(String mname, Object mvalue) {
    if (mname != null) {
      if (members == null) {
	members = new HashMap<String, Object>();
      }
      members.put(mname, mvalue);
    }
  }

  public Object getMemberValue(String mname) {
    if (members != null) {
      return members.get(mname);
    }
    return null;
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("Annotation: @" + name);
    if (value != null) {
      sb.append("(" + value.toString() + ")");
    } else {
      if (members != null && members.size() > 0) {
	sb.append("(");
	Set<Map.Entry<String, Object>> entries = members.entrySet();
	Iterator<Map.Entry<String, Object>> iter = entries.iterator();
	boolean first = true;
	while (iter.hasNext()) {
	  Map.Entry<String, Object> e = iter.next();
	  if (first) {
	    first = false;
	  } else {
	    sb.append(", ");
	  }
	  sb.append(e.getKey()).append("=").append(e.getValue());
	}
	sb.append(")");
      }
    }
    return sb.toString();
  }

  protected String name;

  protected Object value;

  protected Map<String, Object> members;

}
